//
//  JSON.m
//  OCPopular
//
//  Created by Jarvis on 2024/6/4.
//

#import "JSONValue.h"


/// 判断是空， 如：nil、Nil、NSNull、@""、@"<null>"、@[]、@{}、0Data
BOOL isEmpty(id object) {
    if (object == nil || [object isEqual:[NSNull null]]) {
        return YES;
    } else if ([object isKindOfClass:[NSString class]]) {
        if ([object isEqualToString:@""] || [object isEqualToString:@"<null>"]) {
            return YES;
        }
    } else if ([object isKindOfClass:[NSArray class]]) {
        if ([object respondsToSelector:@selector(count)]) {
            return 0==[(NSArray *)object count];
        }
    } else if ([object isKindOfClass:[NSDictionary class]]) {
        if ([object respondsToSelector:@selector(allKeys)]) {
            return 0==[(NSArray *)[object allKeys] count];
        }
    } else if ([object isKindOfClass:[NSData class]]) {
        if ([object respondsToSelector:@selector(length)]) {
            return 0==[(NSData *)object length];
        }
    }
    return NO;
}

/// 非空对象
BOOL isNotEmpty(id object) {
    return !isEmpty(object);
}

/// 非空字符串
BOOL isNotEmptyString(id string) {
    if (isNotEmpty(string)) {
        if([[string class] isSubclassOfClass:[NSString class]]) {
            return YES;
        }
    }
    return NO;
}

/// 非空数组
BOOL isNotEmptyArray(id array) {
    if (isNotEmpty(array)) {
        if([[array class] isSubclassOfClass:[NSArray class]]) {
            return YES;
        }
    }
    return NO;
}

/// 非空字典
BOOL isNotEmptyDictionary(id dictionary) {
    if (isNotEmpty(dictionary)) {
        if([[dictionary class] isSubclassOfClass:[NSDictionary class]]) {
            return YES;
        }
    }
    return NO;
}




/// 字符串读取
/// @param dict 原始数据
/// @param key 目标字段
NSString *_Nullable stringFromDict(NSDictionary *dict, NSString *key) {
    if (isNotEmptyDictionary(dict) && isNotEmptyString(key)) {
        id value = [dict objectForKey:key];
        if (isNotEmpty(value)) {
            if ([value isKindOfClass:[NSString class]]) {
                return value;
            } else if ([value isKindOfClass:[NSNumber class]]) {
                return [(NSNumber *)value stringValue];
            }
        }
    }
    return nil;
}

/// 字典读取
/// @param dict 原始数据
/// @param key 目标字段
NSDictionary * _Nullable dictFromDict(NSDictionary *dict, NSString *key) {
    if (isNotEmptyDictionary(dict) && isNotEmptyString(key)) {
        id value = [dict objectForKey:key];
        if (isNotEmptyDictionary(value)) {
            return value;
        }
    }
    return nil;
}

/// 数组读取
/// @param dict 原始数据
/// @param key 目标字段
NSArray * _Nullable arrayFromDict(NSDictionary *dict, NSString *key) {
    if (isNotEmptyDictionary(dict) && isNotEmptyString(key)){
        id value = [dict objectForKey:key];
        if (isNotEmptyArray(value)) {
            return value;
        }
    }
    return nil;
}

/// 自定义数组读取
/// @param dic 原始数据
/// @param key 目标字段
NSMutableArray * _Nullable arrayFromDictUsingBlock(NSDictionary *dic, NSString *key, id(^usingBlock)(NSDictionary *item)) {
    NSMutableArray *_resultArray = nil;
    NSArray *_temArray = arrayFromDict(dic, key);
    if (isNotEmptyArray(_temArray)) {
        _resultArray = [[NSMutableArray alloc] init];
        for (NSInteger index=0; index<[_temArray count]; index++) {
            NSDictionary *_item = _temArray[index];
            if (usingBlock && isNotEmptyDictionary(_item)) {
                id _model = usingBlock(_temArray[index]);
                if (isNotEmpty(_model)) {
                    [_resultArray addObject:_model];
                }
            }
        }
    }
    return _resultArray;
}








@interface JSONValue ()
@property (nonatomic, weak) NSDictionary *originDict;
@property (nonatomic, weak) id obj;
@end

@implementation JSONValue

+ (instancetype)jsonWithDictionary:(NSDictionary *)dict {
    JSONValue *json = [JSONValue new];
    json.originDict = dict;
    json.obj = dict;
    return json;
}

- (instancetype)objectForKeyedSubscript:(NSString *)aKey {
    if ([_obj isKindOfClass:NSDictionary.class]) {
        _obj = [_obj valueForKeyPath:aKey];
    } else {
        _obj = nil;
    }
    return self;
}

- (instancetype)objectAtIndexedSubscript:(NSUInteger)index {
    if ([_obj isKindOfClass:NSArray.class]) {
        if (index >=0 && index < ((NSArray *)_obj).count) {
            _obj = [_obj objectAtIndex:index];
        } else {
            _obj = nil;
        }
    } else {
        _obj = nil;
    }
    return self;
}

- (instancetype)json {
    _originDict = _obj;
    return self;
}
- (id)rawValue {
    id obj = _obj;
    _obj = _originDict;
    return obj;
}
- (NSString *)stringValue {
    id value = [self rawValue];
    if ([value isKindOfClass:NSString.class]) {
        return value;
    } else if ([value isKindOfClass:NSNumber.class]) {
        return [NSString stringWithFormat:@"%@", value];
    }
    return nil;
}
- (NSInteger)intValue {
    NSString *value = [self stringValue];
    return [value intValue];
}
- (CGFloat)floatValue {
    NSString *value = [self stringValue];
    return [value floatValue];
}
@end
